package user;

import java.util.*;

import edu.cmu.javadb.*;
import static edu.cmu.javadb.plan.condition.Condition.*;

public class GroupReduce {
	public static void main(String[] args) {
		QueryableSet<Person> ps = new QueryableSet<Person>();

		ps.add(new Person("A", 11, 100));
		ps.add(new Person("B", 11, 120));
		ps.add(new Person("C", 12, 130));
		ps.add(new Person("D", 12, 150));

		VTable<Integer, Person> group = ps.groupBy(path("Age"));

		Map<Integer, Integer> res = group.reduce(new ReduceFn<Integer, Person>() {
			public Integer process(Collection<Person> set) {
				int res = 0;
				for (Person p : set) {
					res += p.getHeight();
				}
				return res / set.size();
			}
		});
		
		System.out.println(res.get(11));
		System.out.println(res.get(12));		
	}
}
